module.exports = function ($scope, $state, $resource, $modal, toaster) {

	$scope.load = function (pageNo){
		$scope.searchform.loading = true;
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'code': ''
		}
		$scope.companylist = null;
		$resource('/api/ac/tc/ticketSaleService/getSale3List', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope.companylist = res.data.results;
				$scope.totalItems = res.data.totalRecord;
				$scope.searchform.loading = $scope.companylist.length == 0;
			} else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	};
	
	init();
	function init(){
		$scope.currentPage = 1;      //当前页码
		$scope.itemsPerPage = 10;    //每页显示几条
		$scope.searchform = {
			'loading': true,
			'saleType': {'J': '景区产品', 'M':'商户产品', 'H':'酒店产品'}
		};

		$resource('/api/ac/tc/officeBindService/getAllList', {}, {}).save({'tag': '2'}, function (res) {
			if (res.errcode === 0) {
				$scope.channel_list = res.data.channelList;
				$scope.channel_list.unshift({ name: '--全部渠道--', code: '' });
			} else {
				toaster.error({title: "", body: res.errmsg})
			}
		});
		$resource('api/as/sc/dict/getSaleCategoryList', {}, {}).save({type: 'sale_category'}, function (res) {
			if (res.errcode !== 0) {
				toaster.error({title: "", body: res.errmsg});
				return;
			}
			$scope.dictbytypelist = res.data;
			$scope.dictbytypelist.unshift({label: '---全部---', value: ''})
		});
		$resource('/api/as/tc/placeview/jlist', {}, {}).save({type: 'D'}, function (res) {
			if (res.errcode === 0) {
				$scope.viewarr = res.data;
				$scope.viewarr.unshift({name: '--全部场所--', code: ''});
			} else {
				toaster.error({title: "", body: res.data.errmsg})
			}
		});
		$scope.load(1);
	}

	$scope.apply = function(company){
		if(confirm('确认申请成为: ' + company.create_company_name + ' 的分销商吗？')){
			$resource('/api/ac/sc/office/saveBindOffice2', {}, {})
			  .save({'up_company_code': company.create_company_code}, function (res) {
				if (res.errcode === 0) {
					toaster.success({title: "", body: '申请成功'})
				} else {
					toaster.error({title: "", body: res.data.errmsg});
				}
			});
		}
	}


};